<?php
    global $tb_basepath;
    global $tb_baseurl;
   function SaveCommand($command,$connectionid)
   {
      global $tb_basepath;
      global $tokboxsessionid;
      $file = "$tb_basepath/status/$tokboxsessionid.cmd";
      $fd = fopen ($file , "w");
      if ( ! $fd )
      {
        return null;
      }

      fputs( $fd, "$command:$connectionid:OK");
      fclose ($fd);
      if ( $command == "enlarge" )
      {
      	$file = "$tb_basepath/status/$tokboxsessionid.focus";
      	$fd = fopen ($file , "w");
      	if ( ! $fd )
      	{
        	return null;
      	}

      	fputs( $fd, "$command:$connectionid:OK");
      	fclose ($fd);
      }
   }

   function ReadCommand($cmd)
   {
      global $tb_basepath;
      global $tokboxsessionid;
      if ( $cmd == "FOCUS" )
      	$file = "$tb_basepath/status/$tokboxsessionid.focus";
      else 
      	$file = "$tb_basepath/status/$tokboxsessionid.cmd";

      $fd = fopen ($file , "r");
      if ( ! $fd )
      {
        return "";
      }

      $cmd= fgets( $fd, 1024);
      fclose ($fd);
      return $cmd;
   }

    function recvCommand( $cmd ) {
        return ReadCommand( $cmd );
    }

    function sendCommand($command, $connectionid) {
	SaveCommand($command,$connectionid);
        return "OK";
    }
    
    require("$tb_basepath/lib/sajax.php");
//    $sajax_debug_mode = true;
    $sajax_failure_redirect = "http://sajax.info/sajaxfail.html";
    sajax_export("sendCommand");
    sajax_export("recvCommand");
    sajax_handle_client_request();
     
?> 
<style type="text/css" media="screen">
body {
	background-color: #FFFFFF;
	text-align: left;
	margin: 0 auto;
}
object {
	float:left;
}
#links {
	float:top;
}
#links a {
	display:none;
	color:#000000;
}
</style>

<script type="text/javascript" src="<?="$tb_baseurl/lib/"?>json2.stringify.js"></script>
<script type="text/javascript" src="<?="$tb_baseurl/lib/"?>json_stringify.js"></script>
<script type="text/javascript" src="<?="$tb_baseurl/lib/"?>json_parse_state.js"></script>
<script type="text/javascript" src="<?="$tb_baseurl/lib/"?>sajax.js"></script>
<script type="text/javascript"><!--
    <?php sajax_show_javascript(); ?>
    
    function sendCommand_callback( result) {
	if ( result == "OK" )
	{
		mySession.signal();
	}
	//alert(result);
    }
    
    function recvCommand_callback( result) {
	//alert(result);
	arr=result.split(":",3);
	if ( arr.length !=3 )
	{
		//alert ( "length: " + arr.length);
		return;
	}
	if ( arr[2] != "OK" )
	{
		//alert ( "OK:" + arr[2]);
		return;
	}
	switch ( arr[0])
	{
		case	"disconnect":
			if (arr[1] == mySession.connection.connectionId)
			{
				mySession.disconnect();
			}
			break;
		case	"enlarge":
			enlargeDIV( arr[1] );
			break;
	}
    }

<?
global $displaytype;
if ($displaytype == "subscriber" )
	$show_bigvideo="true";
else
	$show_bigvideo="false";

?>
    var big_subscriber=null;
    var show_bigvideo=<?=$show_bigvideo?>;

function isdefined( variable)
{

	if ( typeof(variable) == "undefined" )
		return False;
	else
		return True;
//    return (typeof(variable) == "undefined")?  false: true;
}

    function enlargeDIV( connectionId ) {
	for ( i=1; i<=divcount; i++)
	{
		if (  cidArray[i] == connectionId ) 
		{
			var newwidth=<?=$videowidth?> + 15;
			var newheight=<?=$videoheight?> + 15;
			resize( divarray[i],newwidth,newheight );
			if ( show_bigvideo == true )
				showBigVideo(divarray[i]);
		}
		else
		{
			var newwidth=<?=$videowidth?> ;
			var newheight=<?=$videoheight?> ;
			resize( divarray[i],newwidth,newheight );

		}
	}
    }
    function showBigVideo( sid ) {
	// unsubscribe old video 
	if ( big_subscriber != null )
	{
		var s = mySession.subscribers[big_subscriber];
		mySession.unsubscribe(s);
	}
    	for(var subscriberid in mySession.subscribers ){
		if ( sid == subscriberid )
		{
			var stream = mySession.subscribers[sid].stream;

			var div = document.getElementById('video_big'  );
                	var divChild = document.createElement('div');
                	var divId = "video_big_child";
                	divChild.setAttribute('id', divId);
                	div.innerHTML="";
                	div.appendChild(divChild);

                        obj = new Object();
                        obj.width=<?=$bv_width?>;
                        obj.height=<?=$bv_height?>;

			var newSubscriber  = mySession.subscribe(stream, divId,obj);
			big_subscriber = newSubscriber.id;
			break;
		}
    	}

	//alert ( subscribers[subscriberid].stream);
    }
    function resize( divid, width, height) {
       var publisherElement = document.getElementById(divid );
       publisherElement.width = width;
       publisherElement.height = height;
    }

    function signalHandler (event ) {
        RecvCommand("CMD");
    }

    function RecvCommand( cmd)
    {
        x_recvCommand( cmd, recvCommand_callback);
    }
    function RetriveFocus( )
    {
        x_recvCommand( "FOCUS", recvCommand_callback);
    }

    function SendCommand(command,id )
    {
	//alert(command + id);
        x_sendCommand(command,id, sendCommand_callback);
    }
    //-->
</script> 


